from machine import Timer
import st7789, utime, math

class gameScene(object):
    def __init__(self, display, bg_clr, circleClr = st7789.WHITE):
        self.background_clr = bg_clr
        self.display = display
        self.clr = circleClr
        self.display.init()
        self.display.fill(st7789.BLACK)

    def Scene(self, display):
        self.display.rect(10, 10, 230, 230, st7789.WHITE)


class gameObj(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def objReflash(self, display, x, y, clr):
        display.circle(x, y, 4, clr)
        

class gameControl(gameObj):
    def __init__(self, *args):
        self.colidMark = 0
        self.exRangeMark = 0
        self.objNum = len(args)
        self.runMark = 0
        self.obj = self.handleArgs(args)
    # def apply_async(func, args, *, callback):
    #     result = func(*args)
    #     callback(result)

    def handleArgs(self, args):
        insNum = self.objNum
        obj = []
        for arg in args:
            obj.append(arg)
        return obj

    def exRangeScan(self, obj):
        for i in range(0, 2):
            mark1 = abs(obj[i].x) < 10 or abs(obj[i].y) < 10
            utime.sleep_us(100)
            mark2 = abs(obj[i].x) > 230 or abs(obj[i].y) > 230
            utime.sleep_us(100)
            if mark1 or mark2:
                if i == 0:
                    self.exRangeMark = 2
                    return 2
                if i == 1:
                    self.exRangeMark = 3
                    return 3
            else:
                self.exRangeMark = 0

    def colidScan(self, obj):
        x1 = obj[0].x
        y1 = obj[0].y
        x2 = obj[1].x
        y2 = obj[1].y
        colidMark = math.sqrt(pow(x1-x2, 2) + pow(y1-y2, 2)) <= 8.0
        if colidMark:
            self.colidMark = 1
            return 1
        else:
            self.colidMark = 0

    def run(self, display, obj, accList, remoteAccList, player):
        x1 = obj[0].x
        y1 = obj[0].y
        x2 = obj[1].x
        y2 = obj[1].y
        print("in run, x1 = {}, y1 = {}".format(x1, y1))
        print("in run, x2 = {}, y2 = {}".format(x2, y2))

        remoteAccList[0] = float(remoteAccList[0])
        remoteAccList[1] = float(remoteAccList[1])

        if self.exRangeScan(obj):
            return self.exRangeMark

        if player == 'player1':
            new_x1 = x1 + int(accList[0])
            new_y1 = y1 - int(accList[1])
            new_x2 = x2 + int(remoteAccList[0])
            new_y2 = y2 - int(remoteAccList[1])
        
        if player == 'player2':
            new_x1 = x1 + int(remoteAccList[0])
            new_y1 = y1 - int(remoteAccList[1])
            new_x2 = x2 + int(accList[0])
            new_y2 = y2 - int(accList[1])

        if self.colidScan(obj):
            if player == 'player1':
                new_x1 = x1 + int(remoteAccList[0]) * 3
                new_y1 = y1 - int(remoteAccList[1]) * 3
                new_x2 = x2 + int(accList[0]) * 3
                new_y2 = y2 - int(accList[1]) * 3
        
            if player == 'player2':
                new_x1 = x1 + int(accList[0]) * 3
                new_y1 = y1 - int(accList[1]) * 3
                new_x2 = x2 + int(remoteAccList[0]) * 3
                new_y2 = y2 - int(remoteAccList[1]) * 3

        self.objReflash(display, x1, y1, st7789.BLACK)
        self.objReflash(display, x2, y2, st7789.BLACK)
        # utime.sleep_ms(10)
        self.objReflash(display, new_x1, new_y1, st7789.WHITE)
        self.objReflash(display, new_x2, new_y2, st7789.CYAN)
        obj[0].x = new_x1
        obj[0].y = new_y1
        obj[1].x = new_x2
        obj[1].y = new_y2

        if self.colidMark == 1:
            return 1

    def init(self, display, obj):
        print("game over!")
        print("2 seconds to restart")
        self.objReflash(display, obj[0].x, obj[0].y, st7789.BLACK)
        self.objReflash(display, obj[1].x, obj[1].y, st7789.BLACK)
        obj[0].x = 60
        obj[0].y = 120
        obj[1].x = 180
        obj[1].y = 120